package VN.EDU.IMIC.DEVELOPER.LETRONGKHANH.QLST;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;

import VN.EDU.IMIC.DEVELOPER.LETRONGKHANH.QLST.Controller.LoginController;
import VN.EDU.IMIC.DEVELOPER.LETRONGKHANH.QLST.Entity.UserEntity;


import com.sqlserver.imic.edu.vn.ConnectionManager.SqlConnection;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.swing.UIManager;
import java.awt.Color;
import javax.swing.JPasswordField;

public class Login extends JFrame {

	private JPanel contentPane;
	private JTextField txtTaiKhoan;
	private JPasswordField txtMatKhau;
	public static UserEntity user; 
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login frame = new Login();
					frame.setVisible(true);
				
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {
		setTitle("ĐĂNG NHẬP HỆ THỐNG");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 533, 213);
		contentPane = new JPanel();
		contentPane.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u0110\u0103ng Nh\u1EADp", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setVisible(true);

		JLabel lblTaiKhoan = new JLabel("Tài Khoản");
		lblTaiKhoan.setBounds(67, 37, 111, 23);
		contentPane.add(lblTaiKhoan);

		txtTaiKhoan = new JTextField();
		txtTaiKhoan.setBounds(173, 38, 250, 20);
		contentPane.add(txtTaiKhoan);
		txtTaiKhoan.setColumns(10);

		JLabel lblMatKhau = new JLabel("Mật Khẩu");
		lblMatKhau.setBounds(67, 68, 111, 23);
		contentPane.add(lblMatKhau);

		JButton btnDangNhap = new JButton("Đăng Nhập");
		btnDangNhap.setHorizontalAlignment(SwingConstants.LEADING);
		btnDangNhap.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Connection objConnection;
				SqlConnection obj = new SqlConnection();

				try {
					objConnection = obj.OpenConnection("2WWFFI9Z5BRIPD9", "sa",
							"12345", "QLST", 1433);
					if (!objConnection.isClosed()) {
						LoginController objController = new LoginController(
								objConnection);
						UserEntity objUser = new UserEntity();
						objUser.setUserName(txtTaiKhoan.getText());
						objUser.setPassWord(txtMatKhau.getText());
						if (objController.CheckUser(objUser)) {
							JOptionPane.showMessageDialog(getParent(),
									"Đăng nhập thành công");
							
						new Main(objUser);
						setVisible(false);
						} else {
							JOptionPane.showMessageDialog(getParent(),
									"Đăng nhập thất bại");
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		btnDangNhap.setIcon(null);
		btnDangNhap.setBounds(173, 124, 113, 23);
		contentPane.add(btnDangNhap);

		JButton btnThoat = new JButton("Thoát");
		btnThoat.setHorizontalAlignment(SwingConstants.LEADING);
		btnThoat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(DO_NOTHING_ON_CLOSE);
			}
		});
		btnThoat
				.setIcon(null);
		btnThoat.setBounds(334, 124, 87, 23);
		contentPane.add(btnThoat);
		
		txtMatKhau = new JPasswordField();
		txtMatKhau.setBounds(173, 71, 250, 20);
		contentPane.add(txtMatKhau);
	}
}
